package it.polimi.swKnights.SWIMv2.servlets.profile;

import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.ejb.personalData.UserFinderRemote;
import it.polimi.swKnights.SWIMv2.general.Errors;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ShowModifyData
 */
public class ShowModifyData extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		String user = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");
		UserFinderRemote userManager = this.lookUp(UserFinderRemote.class);
		String destination = "WEB-INF/users/modify-data/ModifyData.jsp";
		Map<String, String> userMap;
		try
		{
			userMap = userManager.getUserRefereces(user, password, user);
			boolean isPublic = userManager.isPublicUser(user);
			request.setAttribute("isPublic", isPublic);
			request.setAttribute("currentName", userMap.get("name"));
			request.setAttribute("currentSurname", userMap.get("surname"));
			request.setAttribute("currentEmail", userMap.get("email"));
			request.setAttribute("sex", userMap.get("sex"));
			String city = userMap.get("city");
			String street = userMap.get("street");
			String number = userMap.get("number");
			request.setAttribute("city", (city != null) ? city : "<city>");
			request.setAttribute("street", (street != null) ? street
					: "<street>");
			request.setAttribute("number", (number != null) ? number
					: "<number>");

			request.setAttribute("birthDay", userMap.get("birthDay"));
			request.getRequestDispatcher(destination)
					.forward(request, response);
		}
		catch (LoginFailException e)
		{
			response.sendRedirect("./siteHome?error=" + Errors.session);

		}
		catch (InvalidLoginPrivilegesException e)
		{
			response.sendRedirect("./siteHome?error=" + Errors.notuser);
		}

	}

}
